package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.admin.api.entity.PrUserRfidInfoEntity;
import com.pig4cloud.pig.admin.service.PrUserRfidInfoService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 柜体用户标签关联信息
 *
 * @author pig
 * @date 2025-08-02 15:27:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prUserRfidInfo" )
@Tag(description = "prUserRfidInfo" , name = "柜体用户标签关联信息管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PrUserRfidInfoController {

    private final  PrUserRfidInfoService prUserRfidInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param prUserRfidInfo 柜体用户标签关联信息
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_view')" )
    public R getPrUserRfidInfoPage(@ParameterObject Page page, @ParameterObject PrUserRfidInfoEntity prUserRfidInfo) {
        LambdaQueryWrapper<PrUserRfidInfoEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(prUserRfidInfoService.page(page, wrapper));
    }


    /**
     * 通过条件查询柜体用户标签关联信息
     * @param prUserRfidInfo 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_view')" )
    public R getDetails(@ParameterObject PrUserRfidInfoEntity prUserRfidInfo) {
        return R.ok(prUserRfidInfoService.list(Wrappers.query(prUserRfidInfo)));
    }

    /**
     * 新增柜体用户标签关联信息
     * @param prUserRfidInfo 柜体用户标签关联信息
     * @return R
     */
    @Operation(summary = "新增柜体用户标签关联信息" , description = "新增柜体用户标签关联信息" )
    @SysLog("新增柜体用户标签关联信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_add')" )
    public R save(@RequestBody PrUserRfidInfoEntity prUserRfidInfo) {
        return R.ok(prUserRfidInfoService.save(prUserRfidInfo));
    }

    /**
     * 修改柜体用户标签关联信息
     * @param prUserRfidInfo 柜体用户标签关联信息
     * @return R
     */
    @Operation(summary = "修改柜体用户标签关联信息" , description = "修改柜体用户标签关联信息" )
    @SysLog("修改柜体用户标签关联信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_edit')" )
    public R updateById(@RequestBody PrUserRfidInfoEntity prUserRfidInfo) {
        return R.ok(prUserRfidInfoService.updateById(prUserRfidInfo));
    }

    /**
     * 通过id删除柜体用户标签关联信息
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除柜体用户标签关联信息" , description = "通过id删除柜体用户标签关联信息" )
    @SysLog("通过id删除柜体用户标签关联信息" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(prUserRfidInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param prUserRfidInfo 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_export')" )
    public List<PrUserRfidInfoEntity> exportExcel(PrUserRfidInfoEntity prUserRfidInfo,Integer[] ids) {
        return prUserRfidInfoService.list(Wrappers.lambdaQuery(prUserRfidInfo).in(ArrayUtil.isNotEmpty(ids), PrUserRfidInfoEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param prUserRfidInfoList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('admin_prUserRfidInfo_export')" )
    public R importExcel(@RequestExcel List<PrUserRfidInfoEntity> prUserRfidInfoList, BindingResult bindingResult) {
        return R.ok(prUserRfidInfoService.saveBatch(prUserRfidInfoList));
    }
}
